<template>
  <div class="app-container">
    <el-button icon="el-icon-plus" @click="handleAdd">新增配置项</el-button>
    <el-table :data="configList" stripe style="width: 100%">
      <el-table-column prop="configKey" label="配置键" >
      </el-table-column>
      <el-table-column prop="configValue" label="配置值" >
      </el-table-column>
      <el-table-column prop="remark" label="描述" >
      </el-table-column>
      <el-table-column prop="createTime" label="创建时间" >
      </el-table-column>
      <el-table-column prop="updateTime" label="修改时间"> </el-table-column>
      <el-table-column fixed="right" label="操作" >
        <template slot-scope="scope">
          <el-button @click="editRow(scope.row)" type="text" size="small"
            >修改</el-button
          >
          <el-button
            type="text"
            size="small"
            @click.native.prevent="deleteRow(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <el-dialog :title="title" :visible.sync="open" width="780px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col>
            <el-form-item label="配置键" prop="configKey">
              <el-input v-model="form.configKey" placeholder="请输入动态配置键" :disabled="form.id"/>
            </el-form-item>
          </el-col>
          <el-col >
            <el-form-item label="配置值" prop="configValue">
              <el-input v-model="form.configValue" placeholder="请输入动态配置值" />
            </el-form-item>
          </el-col>
          <el-col >
            <el-form-item label="配置值" prop="remark">
              <el-input v-model="form.remark" type="textarea" :rows="2"placeholder="请输入描述" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listConfig, addConfig, editConfig, deleteConfig, getConfigById, getConfigByKey} from "@/api/system/priceMan";

export default {
  data() {
    return {
      // 显示搜索条件
      showSearch: true,
      loading: true,
      configList: [],
      // 表单参数
      form: {},
      title: '',
      open: false,
      // 表单校验
      rules: {
        configKey: [
          { required: true, message: "配置键不能为空", trigger: "blur" }
        ],
        configValue: [
          { required: true, message: "配置值不能为空", trigger: "change" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
     getList() {
      this.loading = true;
      listConfig().then(response => {
        this.configList = response.data;
        this.loading = false;
      });
    },
    reset() {
      this.form = {
        id: undefined,
        configKey: undefined,
        configValue: undefined,
        remark: undefined
      };
      this.resetForm("form");
    },
    deleteRow(row) {
      const id = row.id
      this.$modal.confirm('是否确认删除该配置项？').then(function() {
        return deleteConfig({id:id});
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    editRow(row) {
      this.reset();
      const key = row.configKey
      getConfigByKey({key:key}).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改配置项";
      });
    },

    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加配置项";
    },
    submitForm(){
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            editConfig(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addConfig(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    cancel() {
      this.open = false;
      this.reset();
    }
  },
};
</script>

<style>
</style>